﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Web;

using System.Text;

using System.Configuration;
using DataMongo;
using DataMongo.Entities;
using Quartz;


namespace MVCMongo.Services
{
    public class GetFeedsService : IJob
    {

        //Collects individual message and prepares a single message for email sending

        public void Execute(JobExecutionContext context)
        {
            GetRSSFeeds();
        }


        public static void GetRSSFeeds()
        {
            var sourcedetailconnection = new RepositoryBaseMongo("SourceDetail");
            var list = sourcedetailconnection.GetAll<SourceDetail>();
            var feedConnection = new RepositoryBaseMongo("FeedPost");
           
            foreach (var item in list)
            {
                var data = feedConnection.GetAll<FeedPost>().Select(x => x.SourceLink).ToList();
                var title = feedConnection.GetAll<FeedPost>().Select(x => x.Title).ToList();
                var rssFeeds = RssReader.GetRssFeed(item.FeedURL);
                foreach (var rssFeed in rssFeeds.Where(x => !data.Contains(x.Link) && !title.Contains(x.Title)))
                {
                    var connection = new RepositoryBaseMongo("FeedPost");
                    var feed = new FeedPost()
                    {
                        Title = rssFeed.Title,
                        ShortDescription = rssFeed.Description,
                        FullDescription = rssFeed.Description,
                        SourceId = item.SourceId,
                        CategoryId = item.CategoryId,
                        PublishDate = rssFeed.PublishDate,
                        SourceLink = rssFeed.Link,
                        ImageURL1 = rssFeed.ImageUrl,
                        IsActive = false
                    };
                    var imageUrl = UtilityService.GetImageUrlFromSite(feed.SourceLink);
                    if (imageUrl != null)
                    {
                        var path = AppDomain.CurrentDomain.BaseDirectory + "Content\\FeedImages\\";

                        if (!Directory.Exists(path))
                            Directory.CreateDirectory(path);
                        var imagepath = AppDomain.CurrentDomain.BaseDirectory + "Content\\FeedImages\\" + UtilityService.RemoveSpecialCharacters(feed.Title) + ".jpg";
                        bool result = UtilityService.DownloadImageFromUrl(imageUrl, imagepath);
                        if (result == true)
                        {

                            feed.ImageURL1 = UtilityService.RemoveSpecialCharacters(feed.Title) + ".jpg";
                        }
                        else
                        {
                            feed.ImageURL1 = null;
                        }
                    }
                    else
                    {
                        feed.ImageURL1 = null;
                    }
                    int Id = 0;
                    var feedlist = connection.GetAll<FeedPost>().ToList();
                    if (feedlist.Any())
                        Id = feedlist.Max(x => x.FeedPostId);
                    Id += 1;
                    feed.FeedPostId = Id;
                    connection.InsertOrUpdate(feed);
                }
            }
        }




    }
}